<?php
namespace app\admin\controller\openservice;
use app\common\controller\Admin;
use app\common\logic\ApiNodeLogic;
use app\common\traits\ControllerCurd;
use app\common\annotation\NodeAnnotation;
use think\Request;

/**
 * Class  Openserviceapi
 * @NodeAnnotation(
 *     title="开放api管理",
 *     parent="admin/openservice.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1
 * )
 */
class Openserviceapi extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\openservice\OpenServiceApi();
    }

    /**
     * @NodeAnnotation(
     *     title="api列表",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['title',"like","%$keywords%"]);
        $nodeData = $this->model->where($where)->paginate();

        $nodeList = $nodeData->toArray();

        $newNodeList = [
            "data" => (new ApiNodeLogic())->processNode($nodeList['data']),
            "total" => $nodeList['total']
        ];

        $this->assign([
            "nodeList" => $newNodeList,
            "nodePate" => $nodeData->render(),
            "search" => [
                "keywords" => $keywords
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="导入系api",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function importapi()
    {
        $apps =['api'];
        $app = $this->request->param('app', '');
        if (empty($app) && !empty($apps)) {
            $app = $apps[0];
        }

        if (!in_array($app, $apps)) {
            $this->error('应用' . $app . '不存在!',url('admin/openservice.openserviceapi/index'));
        }

        $newMenus = ApiNodeLogic::importNode($apps);

        $index = array_search($app, $apps);

        $nextIndex = $index + 1;

        $nextIndex = $nextIndex >= count($apps) ? 0 : $nextIndex;

        if ($nextIndex) {
            $this->assign("next_app", $apps[$nextIndex]);
        }

        $this->assign("app", $app);
        $this->assign("new_menus", $newMenus);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="刷新api",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function refapi()
    {
        $apps =[ "api"];
        $app = $this->request->param('app', '');
        if (empty($app)) {
            $app = $apps[0];
        }
        if (!in_array($app, $apps)) {
            $this->error('应用' . $app . '不存在!',url('admin/openservice.openserviceapi/index'));
        }
        $newMenus = ApiNodeLogic::updateNode($apps);

        $index = array_search($app, $apps);

        $nextIndex = $index + 1;

        $nextIndex = $nextIndex >= count($apps) ? 0 : $nextIndex;

        if ($nextIndex) {
            $this->assign("next_app", $apps[$nextIndex]);
        }

        $this->assign("app", $app);
        $this->assign("new_menus", $newMenus);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="清楚异常api",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function clearExceptionapi()
    {
        $nodeList = $this->model->getNodeList()->toArray();

        $nodeList = (new ApiNodeLogic())->processNode($nodeList);

        $result = $this->model->clearExceptionNode($nodeList);

        if ($result)
        {
            $this->success("清楚成功",url('admin/openservice.openserviceapi/index'));
        } elseif (is_array($result) and empty($result)) {
            $this->error("暂无所需要清楚的节点");
        }else{
            $this->error("清楚失败");
        }
    }

    /**
     * @NodeAnnotation(
     *     title="添加api",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function addapi(Request $request)
    {
        if($request->isAjax() and $request->isPost())
        {
            $dataType = $request->param("datasType");
            if($dataType && $dataType=='tree')
            {
                $TreeMenu = $this->model->select()->toArray();
                return json($TreeMenu);
            }
        }
        $this->add(url('admin/openservice.openserviceapi/index'));
        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="编辑api",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function editapi(Request $request,$id=0)
    {
        if($request->isAjax() and $request->isPost())
        {
            $dataType = $request->param("datasType");
            $apiNodes = $this->model->select()->toArray();

            if($dataType && $dataType=='tree')
            {
                return json($apiNodes);
            }
        }
        $this->edit(url('admin/openservice.openserviceapi/index'));

        $api = $this->model->findNode($id);

        $this->assign([
            "api"  => $api,
            "cate" => $this->model->findNode($api['parent_id'])
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除api",
     *     parent="admin/openservice.openserviceapi",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function delapi(Request $request)
    {
        $this->del();
    }
}
